package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.StringUtils;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysFeedback;
import cn.wgx.modules.sys.entity.SysFeedbackExample;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.mapper.SysFeedbackMapper;
import cn.wgx.modules.sys.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysFeedbackService extends BaseService<SysFeedback> {

    @Resource
    SysFeedbackMapper sysFeedbackMapper;

    @Transactional(readOnly = true)
    public List<SysFeedback> getList(SysFeedback sysFeedback) {
        SysFeedbackExample example = new SysFeedbackExample();
        SysFeedbackExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(sysFeedback.getTitle())) {
            criteria.andTitleLike("%" + sysFeedback.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(sysFeedback.getContent())) {
            criteria.andContentLike("%" + sysFeedback.getContent() + "%");
        }
        if (sysFeedback.getCreateUserId() != null) {
            criteria.andCreateUserIdEqualTo(sysFeedback.getCreateUserId());
        }
        example.setOrderByClause("createDate desc");
        return sysFeedbackMapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysFeedback> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysFeedbackExample example = new SysFeedbackExample();
            SysFeedbackExample.Criteria criteria = example.createCriteria();
            criteria.andFeedback_idIn(uniList);
            List<SysFeedback> eduCourseAssignmentHis = sysFeedbackMapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    public SysFeedback getOne(Integer id) {
        return sysFeedbackMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int insertOne(SysFeedback sysFeedback) {
        filter(sysFeedback, true);
        return sysFeedbackMapper.insertSelective(sysFeedback);
    }

    @Transactional
    public Object update(Integer feedback_id, String answer ) {
        SysFeedback sysFeedback = new SysFeedback();
        sysFeedback.setFeedback_id(feedback_id);
        sysFeedback.setAnswer(answer);
        sysFeedback.setHasRead(true);
        filter(sysFeedback, false);
        return sysFeedbackMapper.updateByPrimaryKeySelective(sysFeedback);
    }


    @Transactional
    public Object update(Integer feedback_id) {
        SysFeedback sysFeedback = new SysFeedback();
        sysFeedback.setFeedback_id(feedback_id);
        sysFeedback.setHasRead(true);
        filter(sysFeedback, false);
        return sysFeedbackMapper.updateByPrimaryKeySelective(sysFeedback);
    }

    @Transactional
    public int del(Integer id) {
        return sysFeedbackMapper.deleteByPrimaryKey(id);
    }

    public void filter(SysFeedback sysFeedback, boolean isInsert){
        SysUser user = UserUtil.getUser();
        if(isInsert){
            if(null != user){
                sysFeedback.setCreate_username(user.getNickname());
            }
            sysFeedback.setAnswer_username(null);
            sysFeedback.setAnswer(null);
            sysFeedback.setHasRead(false);
            sysFeedback.setPid(0);
        }else{
            if(null != user && StringUtils.isNotBlank(sysFeedback.getAnswer())){
                sysFeedback.setAnswer_username(user.getNickname());
            }
            sysFeedback.setCreate_username(null);
            sysFeedback.setPid(null);
            sysFeedback.setTitle(null);
            sysFeedback.setContent(null);
            sysFeedback.setFile(null);
        }
        super.filter(sysFeedback, isInsert);
    }

}
